package com.dome.base.utils;

import cn.hutool.core.text.StrBuilder;
import cn.hutool.core.util.RandomUtil;
import com.dome.base.constant.TokenConstants;
import com.dome.redis.utils.JedisUtils;

public class JedisTokenUtil {
    public static String createToken() {
        return createToken(0);
    }

    public static String getKey(String token) {
        StrBuilder key = new StrBuilder();
        key.append(TokenConstants.Redis.SUBMIT_TOKEN_PREFIX).append(token);
        return key.toString();
    }

    public static String createToken(int cacheSeconds) {
        String str = RandomUtil.randomString(16);
        String token = getKey(str);
        if(cacheSeconds==0) {
            cacheSeconds = TokenConstants.Redis.EXPIRE_TIME_MINUTE;
        }
        JedisUtils.set(token, token, cacheSeconds);

        return str;
    }
    /**
     * 删除token
     * @param
     * @return long
     * @since 1.0.0
     * @author jinjie.chen
     * 2020/12/18 15:32
     */
    public static long delToken() {
        String str = RandomUtil.randomString(32);
        StrBuilder token = new StrBuilder();
        token.append(TokenConstants.Redis.SUBMIT_TOKEN_PREFIX).append(str);
        long result = JedisUtils.del(token.toString());

        return result;
    }
    /**
     * 是否存在token
     * @param
     * @return long
     * @since 1.0.0
     * @author jinjie.chen
     * 2020/12/18 15:32
     */
    public static boolean existsToken(String key) {
        StrBuilder token = new StrBuilder();
        token.append(TokenConstants.Redis.SUBMIT_TOKEN_PREFIX).append(key);

        return JedisUtils.exists(token.toString());
    }
    /**
     * 删除token
     * @param
     * @return long
     * @since 1.0.0
     * @author jinjie.chen
     * 2020/12/18 15:32
     */
    public static long delToken(String key) {
        StrBuilder token = new StrBuilder();
        token.append(TokenConstants.Redis.SUBMIT_TOKEN_PREFIX).append(key);
        long result = JedisUtils.del(token.toString());

        return result;
    }
}
